package com.ymatou.iapi.couponmanage.old.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponmanage.old.parameter.BatchInfo;
import com.ymatou.iapi.couponmanage.old.parameter.GetBatchInfoBean;
import com.ymatou.iapi.couponmanage.old.service.GetBatchInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
/**
*获取批次信息用例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetBatchInfo{
	private static GetBatchInfoBean getbatchinfoBean;
	private static GetBatchInfoCall getbatchinfoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取批次信息用例");
	}

	@Before
	public void caseUp() {
		getbatchinfoBean=new GetBatchInfoBean();
		getbatchinfoCall=new GetBatchInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getbatchinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchInfo_001() {
		Logger.start(true,"获取批次信息-短码");
		try {
			List<Map> list = new YmtCouponBatchWapper().getTop1PlatformShortCodeCouponInfo();

			getbatchinfoBean.setBatchCode(list.get(0).get("sBatchCode").toString());
			getbatchinfoCall.setData(getbatchinfoBean);
			getbatchinfoCall.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchInfo_002() {
		Logger.start(true,"获取批次信息-链接");
		try {
			List<Map> list = new YmtCouponBatchWapper().getTop1PlatformLinkCouponInfo();

			getbatchinfoBean.setBatchCode(list.get(0).get("sBatchCode").toString());
			getbatchinfoCall.setData(getbatchinfoBean);
			getbatchinfoCall.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchInfo_003() {
		Logger.start(true,"获取批次信息-商家优惠券");
		try {
			List<Map> list = new YmtCouponBatchWapper().getTop1SellerCouponInfo();

			getbatchinfoBean.setBatchCode(list.get(0).get("sBatchCode").toString());
			getbatchinfoCall.setData(getbatchinfoBean);
			getbatchinfoCall.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void VerifyReturn(Map item) {
        Logger.verifyEquals("0", getbatchinfoCall.getCode(),
                "验证返回code");
     
        Logger.verifyEquals("操作成功！",
                getbatchinfoCall.getMessage(), "验证返回Message");
        
        BatchInfo batchInfo = getbatchinfoCall.getBatchInfo();

        String batchCode = getbatchinfoBean.getBatchCode();

        Logger.verifyEquals(Integer.parseInt(item.get("iBatchId").toString()),
        		batchInfo.getBatchId(),
                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中iBatchId字段相同");
        
        Logger.verifyEquals(item.get("sBatchCode").toString(),
        		batchInfo.getBatchCode(),
                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中sBatchCode字段相同");

        // 是否作废
        if(item.get("bInvalidStatus")==null)
        {
        	Logger.verifyEquals(false,
            		batchInfo.getIsCancellation(),
                    "验证返回IsCancellation与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                            + "记录中bInvalidStatus字段相同");
        }
        else {
        	Logger.verifyEquals(Boolean.parseBoolean(item.get("bInvalidStatus").toString()),
            		batchInfo.getIsCancellation(),
                    "验证返回IsCancellation与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                            + "记录中bInvalidStatus字段相同");
		}
        

     // 是否过期
        if(item.get("iEffectiveType")!=null && Integer.parseInt(item.get("iEffectiveType").toString())==1)
        {
        	Date date =new Date(item.get("dValidEnd").toString());
        	
        	Logger.verifyEquals(date.before(new Date()),
        		batchInfo.getIsExpire(),
                "验证返回IsExpire正确");
        }
        
     // 是否领完
        Logger.verifyEquals(Integer.parseInt(item.get("iCouponTotalNum").toString())<Integer.parseInt(item.get("iReceiveCount").toString()),
        		batchInfo.getIsReceiveOver(),
                "验证返回IsReceiveOver正确");
        
   
        Logger.verifyEquals(Integer.parseInt(item.get("iSendType").toString()),
        		batchInfo.getSendType(),
                "验证返回SendType与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中iSendType字段相同");
        
        if(item.get("iEffectiveType")==null)
        {
        	Logger.verifyEquals(0,
        		batchInfo.getEffectiveType(),
                "验证返回EffectiveType与数据库中表Ymt_CouponSetting中iEffectiveType字段相同");
        }
        else{
        	   Logger.verifyEquals(Integer.parseInt(item.get("iEffectiveType").toString()),
               		batchInfo.getEffectiveType(),
                       "验证返回EffectiveType与数据库中表Ymt_CouponSetting中iEffectiveType字段相同");
        }
        
        if(item.get("iEffectiveType")==null)
        {
        	 Logger.verifyEquals(0,
             		batchInfo.getEffectiveValidType(),
                     "验证返回EffectiveValidType与数据库中表Ymt_CouponSetting中iEffectiveValidType字段相同");
             }
        else
        {
        	 Logger.verifyEquals(Integer.parseInt(item.get("iEffectiveValidType").toString()),
             		batchInfo.getEffectiveValidType(),
                     "验证返回EffectiveValidType与数据库中表Ymt_CouponSetting中iEffectiveValidType字段相同");
        
        }

        Logger.verifyEquals(item.get("dValidStart").toString().subSequence(0, 19),
        		batchInfo.getValidStart().replace("T", " "),
                "验证返回dValidStart与数据库中表Ymt_CouponSetting中dValidStart字段相同");

        Logger.verifyEquals(item.get("dValidEnd").toString().subSequence(0, 19),
        		batchInfo.getValidEnd().replace("T", " "),
                "验证返回dValidEnd与数据库中表Ymt_CouponSetting中dValidEnd字段相同");
        
        if(item.get("iEffectiveDays")==null)
        {
        	Logger.verifyEquals(0,
        		batchInfo.getEffectiveDays(),
                "验证返回iEffectiveDays与数据库中表Ymt_CouponSetting中iEffectiveDays字段相同");
        }
        else {
        	Logger.verifyEquals(Integer.parseInt(item.get("iEffectiveDays").toString()),
            		batchInfo.getEffectiveDays(),
                    "验证返回iEffectiveDays与数据库中表Ymt_CouponSetting中iEffectiveDays字段相同");     
		}
        
        Logger.verifyEquals(Integer.parseInt(item.get("iCouponTotalNum").toString()),
        		batchInfo.getTotalCount(),
                "验证返回TotalCount与数据库中表Ymt_CouponBatch中iCouponTotalNum字段相同");
        
        Logger.verifyEquals(Integer.parseInt(item.get("iReceiveCount").toString()),
        		batchInfo.getReceivedCount(),
                "验证返回BatchCode与数据库中表Ymt_CouponSetting中iReceiveCount字段相同");
        
        if(item.get("IsShowInPage")==null)
        {
        	Logger.verifyEquals(false,
        		batchInfo.getIsShowInPage(),
                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中IsShowInPage字段相同");
        }
        else {
            Logger.verifyEquals(Boolean.parseBoolean(item.get("IsShowInPage").toString()),
            		batchInfo.getIsShowInPage(),
                    "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                            + "记录中IsShowInPage字段相同");
		}
        
        Logger.verifyEquals(Integer.parseInt(item.get("iUserType").toString()),
        		batchInfo.getUserType(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中iUserType字段相同");
        
        if(item.get("sSellerIds")==null)
        {
        	Logger.verifyEquals(null,
        		batchInfo.getSellerIds(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中sSellerIds字段相同");
        }
        else {
        	Logger.verifyEquals(item.get("sSellerIds").toString(),
            		batchInfo.getSellerIds(),
                    "验证返回BatchCode与数据库中表Ymt_CouponScenario中sSellerIds字段相同");
		}
        
        if(item.get("sProductCategories")==null)
        {
        	Logger.verifyEquals(null,
        		batchInfo.getProductCategories(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中sProductCategories字段相同");
        }
        else {
        	Logger.verifyEquals(item.get("sProductCategories").toString(),
            		batchInfo.getProductCategories(),
                    "验证返回BatchCode与数据库中表Ymt_CouponScenario中sProductCategories字段相同");
		}
        
        if(item.get("sSpecificProducts")==null)
        {
        	Logger.verifyEquals(null,
        		batchInfo.getSpecificProducts(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中sSpecificProducts字段相同");
        }
        else {
        	Logger.verifyEquals(item.get("sSpecificProducts").toString(),
            		batchInfo.getSpecificProducts(),
                    "验证返回BatchCode与数据库中表Ymt_CouponScenario中sSpecificProducts字段相同");
		}
        
        if(item.get("sProductBrands")==null)
        {
        	Logger.verifyEquals(null,
        		batchInfo.getProductBrands(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中sProductBrands字段相同");
        }
        else {
        	 Logger.verifyEquals(item.get("sProductBrands").toString(),
             		batchInfo.getProductBrands(),
                     "验证返回BatchCode与数据库中表Ymt_CouponScenario中sProductBrands字段相同");
          
		}
        
        if(item.get("sActivityIds")==null)
        {
        	Logger.verifyEquals(null,
        		batchInfo.getActivityIds(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中sActivityIds字段相同");
        }
        else {
        	  Logger.verifyEquals(item.get("sActivityIds").toString(),
              		batchInfo.getActivityIds(),
                      "验证返回BatchCode与数据库中表Ymt_CouponScenario中sActivityIds字段相同");        
		}
        
        if(item.get("sUsePlatforms")==null)
        {
        	Logger.verifyEquals(null,
        		batchInfo.getUsePlatforms(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中sUsePlatforms字段相同");
        }
        else {
        	Logger.verifyEquals(item.get("sUsePlatforms").toString(),
            		batchInfo.getUsePlatforms(),
                    "验证返回BatchCode与数据库中表Ymt_CouponScenario中sUsePlatforms字段相同");
		}

    }
}